using System.Collections.Generic;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 帮助命令
    /// 用于获取命令帮助信息
    /// 作者：huobing
    /// </summary>
    public sealed class HelpCommand : BaseCommand<HelpResult>
    {
        public const string NAME = "HELP";
        public override string Name => NAME;
        public override string Format => "HELP [命令名]";
        public override string Description => "获取命令帮助信息";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error)
        {
            error = string.Empty;
            return args.Length <= 1 || (error = "命令格式错误，HELP命令最多接受一个参数") == string.Empty;
        }
    }
    /// <summary>
    /// 命令帮助项类
    /// 存储单个命令的帮助信息
    /// 作者：huobing
    /// </summary>
    public class CommandItem
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public string Name { get; set; } = string.Empty;
        
        /// <summary>
        /// 命令格式
        /// </summary>
        public string Format { get; set; } = string.Empty;
        
        /// <summary>
        /// 命令描述
        /// </summary>
        public string Description { get; set; } = string.Empty;
        
        /// <summary>
        /// 命令权限
        /// </summary>
        public string Permission { get; set; } = string.Empty;
        
        /// <summary>
        /// 命令参数说明
        /// </summary>
        public string ParameterDescription { get; set; } = string.Empty;
    }

    /// <summary>
    /// 帮助命令执行结果
    /// 作者：huobing
    /// </summary>
    public class HelpResult : BaseCommandResult
    {
        /// <summary>
        /// 命令帮助信息列表
        /// </summary>
        public List<CommandItem> Commands { get; set; }

        /// <summary>
        /// 是否为特定命令的帮助
        /// </summary>
        public bool IsSpecificCommandHelp { get; set; }

        /// <summary>
        /// 查询的命令名称
        /// </summary>
        public string? RequestedCommand { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public HelpResult()
        {
            Commands = new List<CommandItem>();
        }
        
        public void AddCommandItem(CommandItem commandItem)
        {
            Commands.Add(commandItem);
        }
        
        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的帮助信息</returns>
        public override string ToConsoleString()
        {  
            var sb = new System.Text.StringBuilder();
            if (Commands == null || Commands.Count == 0)
            {
                if (IsSpecificCommandHelp && !string.IsNullOrEmpty(RequestedCommand))
                {
                    sb.AppendLine($"未找到命令 '{RequestedCommand}' 的帮助信息");
                }
                else
                {
                    sb.AppendLine("暂无可用命令");
                }
            }
            else if (IsSpecificCommandHelp)
            {
                // 特定命令的详细帮助
                var command = Commands[0];
                sb.AppendLine($"命令: {command.Name}");
                sb.AppendLine($"格式: {command.Format}");
                sb.AppendLine($"描述: {command.Description}");
                sb.AppendLine($"权限: {command.Permission}");
                if (!string.IsNullOrEmpty(command.ParameterDescription))
                {
                    sb.AppendLine($"参数说明: {command.ParameterDescription}");
                }
            }
            else
            {
                // 所有命令的列表帮助
                sb.AppendLine("可用命令列表：");
                
                // 准备表格数据
                var tableData = new List<string[]>();
                tableData.Add(new[] { "命令", "描述"}); // 表头
                
                foreach (var command in Commands)
                {
                    tableData.Add(new[]
                    {
                        command.Name,
                        command.Description
                    });
                }
                
                // 使用TextTableFormatter格式化表格
                string table = LDNS.SDK.Formatting.TextTableFormatter.FormatAlignedTable(tableData, ' ', 120);
                sb.AppendLine(table);
                
                sb.AppendLine("\n输入 'HELP 命令名' 获取特定命令的详细帮助");
            }
            
            return sb.ToString().TrimEnd();
        }
    }
}